`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company: 
// Engineer: 
// 
// Create Date:    18:48:42 03/31/2014 
// Design Name: 
// Module Name:    debouncer 
// Project Name: 
// Target Devices: 
// Tool versions: 
// Description: 
//
// Dependencies: 
//
// Revision: 
// Revision 0.01 - File Created
// Additional Comments: 
//
//////////////////////////////////////////////////////////////////////////////////
module debouncer(button, clk, i_increment);
	input button;
	input clk;                     //v10
	
output reg i_increment;              //output value is 0 or 1
	
reg [1:0] state, next_state;
	reg [30:0] count;                     //wait period before we accept next input
  
  

always@(posedge clk) begin  
		state <= next_state;
	end
	
	always @ (posedge clk) begin				//state 0 is output 0, idle

      case(state)                         //state 1 is output 1, count=0

          2'b00: begin                    //state 2 is ouput 0, count=count+1

              if (button==1)
                  next_state <= 1;

              else
                  next_state <= 0;

              end

          2'b01: begin
                  next_state <= 2;

              end

          2'b10: begin

              if(count >= 31'd900)
                  next_state <= 0;

              else
                  next_state <= 2;

              end
          default: next_state <= 0;

		endcase
	end
	
	always @ (posedge clk) begin
		case (state)

			2'b00: begin
				i_increment <= 0;

				count <= 0;

			end

			2'b01: begin
				i_increment <= 1;

				count <= 0;
			end

			2'b10: begin
				i_increment <= 0;
				count <= count + 1'b1;
			end
			default: begin 
				i_increment <= 0;
				count <= 0;
			end
      endcase

  end
            
endmodule
